package cn.tedu.charging.order.rabbitmq;

import cn.tedu.charging.order.common.OrderStatusConstants;
import cn.tedu.charging.order.config.RabbitMQConfiguration;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.pojo.dto.OrderMQDto;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class RabbitMQConsumer {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

//    @RabbitListener(queues = RabbitMQConfiguration.QUEUE_NAME)
    @RabbitListener(queues = RabbitMQConfiguration.DEAD_LETTER_QUEUE_NAME)
    public void receive(OrderMQDto orderMQDto, Message message, Channel channel)
    {
        System.out.println("接收到消息："+orderMQDto);
        ChargingOrderSuccessPO chargingOrderSuccessPO = new ChargingOrderSuccessPO();
        chargingOrderSuccessPO.setBillStatus(OrderStatusConstants.ORDER_STATUS_EXCEPTION_END);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bill_id",orderMQDto.getOrderNo());
        updateWrapper.eq("bill_status",OrderStatusConstants.ORDER_STATUS_PROCESS);
        int row = orderSuccessMapper.update(chargingOrderSuccessPO,updateWrapper);
        if (row == 0){
            log.debug("订单正常结束");
        }else {
            log.debug("订单异常结束");
        }

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            log.error("确认消息失败，消息ID:{},异常信息:{}",message.getMessageProperties().getDeliveryTag(),e);
        }
    }
}
